package com.tjetc.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/2
 */
@Repository
public class MonsterDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 默认回滚方式 是RuntimeException
    // rollbackFor: 将默认回滚的异常 扩大成Exception
    // 考虑事务的传播机制：当调用多个方法时，每个方法中都有Transactional注解时
    @Transactional(rollbackFor = Exception.class)
    public void addMonster(){
        String sql = "insert into db.monster (name, skill) VALUES ('zhangsan', 'run2')";
        int row = jdbcTemplate.update(sql);
        int i = 1/ 0;
        int row1 = jdbcTemplate.update(sql);
//        System.out.println(row);
    }
}